<template>
  <div class="container">
    <!-- 搜索条件-->
    <el-form ref="queryForm" :inline="true" :model="query" class="demo-form-inline">
      <el-form-item label="管理员名称" prop="loginUserName">
        <el-input v-model="query.loginUserName" placeholder="请输入管理员名称" />
      </el-form-item>

      <el-form-item label="权限标识" prop="perms">
        <el-input v-model="query.perms" placeholder="请输入权限标识" />
      </el-form-item>

      <el-form-item label="状态" prop="locked">
        <el-select v-model="query.locked" placeholder="请选择" clearable>
          <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="search">查询</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="small" plain @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="管理员编号" prop="adminUserId">
          <el-input v-model="ruleForm.adminUserId" />
        </el-form-item>
        <el-form-item label="管理员名称" prop="loginUserName">
          <el-input v-model="ruleForm.loginUserName" />
        </el-form-item>
        <el-form-item label="管理员昵称" prop="nickName">
          <el-input v-model="ruleForm.nickName" />
        </el-form-item>
        <el-form-item label="权限字符" prop="perms">
          <el-input v-model="ruleForm.perms" />
        </el-form-item>
        <el-form-item label="状态" prop="locked">
          <el-select v-model="ruleForm.locked" placeholder="请选择状态">
            <el-option label="未锁定" value="0" />
            <el-option label="已锁定" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">确定</el-button>
          <el-button @click="cancel()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>
    <!-- 画表格-->
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="adminUserId" label="管理员编号" width="100" />
      <el-table-column prop="loginUserName" label="管理员名称" width="250" />
      <el-table-column prop="nickName" label="管理员昵称" width="150" />
      <el-table-column prop="perms" label="权限字符" width="250" />
      <el-table-column prop="locked" label="状态" width="150">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.locked"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 画分页条 -->
    <el-pagination
      v-show="total>0"
      :current-page="query.page"
      :page-sizes="[10, 20, 30, 50,100]"
      :page-size="query.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<!--, getUser-->
<script>
import { getData, updateUser, addUser, delUser } from '@/api/user'

export default {
  name: 'User',
  data() {
    return {
      query: {
        loginUserName: undefined,
        perms: undefined,
        status: undefined,
        page: 1,
        size: 5
      },
      ruleForm: {},
      title: '',
      createDate: [],
      statusList: [
        { label: '未锁定', value: 0 },
        { label: '已锁定', value: 1 }
      ],
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      rules: {
        loginUserName: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '昵称不能为空', trigger: 'blur' }
        ],
        perms: [
          { required: true, message: '权限字符不能为空', trigger: 'blur' }
        ],
        locked: [
          { required: true, message: '状态不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getInfoData()
  },
  methods: {
    reset() {
      this.ruleForm = {
        adminUserId: undefined,
        loginUserName: undefined,
        nickName: undefined,
        perms: undefined,
        locked: undefined
      }
      this.resetForm('ruleForm')
    },
    submitForm() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          if (this.ruleForm.adminUserId !== undefined) {
            updateUser(this.ruleForm).then(resp => {
              this.dialogFormVisible = false
              this.getInfoData()
            })
          } else {
            addUser(this.ruleForm).then(resp => {
              this.dialogFormVisible = false
              this.getInfoData()
            })
          }
        }
      })
    },
    handleStatusChange(row) {
      row.locked === '0' ? '未锁定' : '已锁定'
    },
    cancel() {
      this.dialogFormVisible = false
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.getInfoData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.getInfoData()
    },
    search() {
      this.query.page = 1
      this.getInfoData()
    },
    resetForm() {
      this.$refs['queryForm'].resetFields()
      this.search()
    },
    handleAdd() {
      this.title = '新增数据'
      this.dialogFormVisible = true
      this.reset()
      // addUser().then(resp => {
      //   // console.log(resp)
      // })
    },
    handleDelete(row) {
      console.log(row)
      const userId = row.adminUserId
      this.$confirm('是否确认删除用户编号为“' + userId + '"数据项？').then(resp => {
        return delUser(userId)
      }).then(() => {
        this.getInfoData()
      }).catch(() => {})
    },
    handleEdit(row) {
      // console.log(row)
      // this.title = '修改数据'
      this.dialogFormVisible = true
      // this.reset()
      this.ruleForm = row
      updateUser(this.ruleForm).then(resp => {})
    },
    getInfoData() {
      getData(this.query).then(resp => {
        // console.log(resp)
        this.total = resp.count
        this.tableData = resp.data
      })
    }
  }
}
</script>

<style scoped>

</style>
